//
//  YJMNewCell.swift
//  Weibo
//
//  Created by 杨军民 on 16/7/22.
//  Copyright © 2016年 杨军民. All rights reserved.
//

import UIKit


/*
1、懒加载背景图片
(添加：懒加载分享按钮和开始按钮)
2、设置UI界面
a、添加子控件
b、约束
3、cell显示图片
4、分享按钮和开始按钮点击事件
a、开始按钮跳转界面
5、隐藏按钮

*/

class YJMNewCell: UICollectionViewCell {

    
    //cell显示图片
    var image : UIImage? {
        didSet{
            backImage.image = image
        }
    }
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    override init(frame: CGRect) {
        super.init(frame:frame)
        //调用
        setUpUI()
    }
    
    //MARK: - 公开方法
    func showButton(isShow : Bool) {
        sharedButton.hidden = !isShow
        startButton.hidden = !isShow
    }
    
    private func setUpUI() {
        //添加子控件
        self.contentView.addSubview(backImage)
        self.contentView.addSubview(sharedButton)
        self.contentView.addSubview(startButton)
        
        //约束
        backImage.snp_makeConstraints { (make) -> Void in
            make.edges.equalTo(self.contentView)
        }
        
        //开始按钮
        startButton.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self.contentView)
            make.bottom.equalTo(self.contentView).offset(-160)
        }
        
        //分享按钮
        sharedButton.snp_makeConstraints { (make) -> Void in
            make.centerX.equalTo(self.contentView)
            make.bottom.equalTo(startButton.snp_top).offset(-16)
        }
    }
    
    //MARK: - 点击事件
    //分享事件
    @objc private func sharedButtonClick() {
        //切换状态
        sharedButton.selected = !sharedButton.selected
    }
    
    //开始按钮事件
    @objc private func startButtonClick() {
        
        print(__FUNCTION__)
        //切换接界面
        AppDelegate.switchRootViewController(YJMMainTableViewController())
    }
    
    //MARK: -懒加载背景图片
    //背景
    private lazy var backImage : UIImageView = UIImageView (image: UIImage(named: "new_feature_1"))
    
    //勾选按钮
    private lazy var sharedButton : UIButton = {
       let button = UIButton(type: UIButtonType.Custom)
        
        //设置标题
        button.setTitle("分享给大家", forState: UIControlState.Normal)
        //设置普通状态的背景图片
        button.setImage(UIImage(named: "new_feature_share_false"), forState: UIControlState.Normal)
        //设置高亮状态的背景图片
        button.setImage(UIImage(named: "new_feature_share_true"), forState: UIControlState.Highlighted)
        //设置字体颜色
        button.setTitleColor(UIColor .blackColor(), forState: UIControlState.Normal)
        
        //按钮点击事件
        button.addTarget(self, action: "sharedButtonClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        return button
    }()
    
    //开始按钮
    private lazy var startButton : UIButton = {
        let button = UIButton (type: UIButtonType.Custom)
        
        //设置标题
        button.setTitle("开始体验", forState: UIControlState.Normal)
        //设置普通状态的背景图片
        button.setBackgroundImage(UIImage(named: "new_feature_finish_button"), forState: UIControlState.Normal)
        //设置高亮状态的背景图片
        button.setBackgroundImage(UIImage(named: "new_feature_finish_button_highlighted"), forState: UIControlState.Highlighted)
        //点击事件
        button.addTarget(self, action: "startButtonClick", forControlEvents: UIControlEvents.TouchUpInside)
        
        return button
    }()
}
